import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/storage'
import { Message } from 'element-ui'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/dashboard',
        component: () => import('@/views/layout'),
        children: [
            {path: '/dashboard', component: () => import('@/views/dashboard')},
            {path: '/article', component: () => import('@/views/article')},
        ]
    },
    {path: '/login', component: () => import('@/views/login')}
]

const router = new VueRouter({
  routes
})

const whiteList = ['/login']
router.beforeEach((to, from, next) => {
    if (getToken()) {
        next()
    } else if (whiteList.includes(to.path)) {
        next()
    } else {
        Message.error('请您先登录！')
        next('/login')
    }
})
export default router
